package com.xsf.jieyou.cloud.service;

import com.xsf.jieyou.cloud.api.CloudRedisQrcodeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Redis 二维码 服务接口实现类
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/11/19 17:01
 * @see com.xsf.jieyou.cloud.service
 **/
@Service(version = "1.0.0")
@RefreshScope
public class CloudRedisQrcodeServiceImpl implements CloudRedisQrcodeService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 基础 Redis KEY
     */
    @Value("${redis_key_qrcode}")
    private String baseRedisKey;
    /**
     * 二维码过期时间(单位：S)
     */
    @Value("${redis_key_qrcode_expire_second}")
    private Integer redisKeyQrcodeRegisterExpireSecond;

    /**
     * 存
     *
     * @param code  {@link String} 唯一标识
     * @param state {@link String} 二维码状态,-1 授权失败 0 未扫描 1 扫描成功 2 授权成功  null 过期
     * @return {@link Integer}
     */
    @Override
    public Integer insertRedisQrCode(String code, String state) {
        String key = baseRedisKey + ":" + code;
        this.redisTemplate.opsForValue().set(key, state, redisKeyQrcodeRegisterExpireSecond, TimeUnit.SECONDS);
        return 200;
    }

    /**
     * 取
     *
     * @param code {@link String} 唯一标识
     * @return {@link String} 二维码状态,-1 授权失败 0 未扫描 1 扫描成功 2 授权成功  null 过期
     */
    @Override
    public String selectRedisQrCode(String code) {
        String key = baseRedisKey + ":" + code;
        return (String) this.redisTemplate.opsForValue().get(key);
    }

    /**
     * 删
     *
     * @param code {@link String} 唯一标识
     * @return {@link Integer}
     */
    @Override
    public Integer deleteRedisQrCode(String code) {
        String key = baseRedisKey + ":" + code;
        this.redisTemplate.delete(key);
        return 200;
    }
}
